package main

import (
	"fmt"
	"os"
)

func main() {
	var env, module string
	if len(os.Args) == 4 && os.Args[1] == "-cd" {
		env = os.Args[2]
		module = os.Args[3]
	} else if len(os.Args) == 3 {
		env = os.Args[1]
		module = os.Args[2]
	} else {
		fmt.Println("Usage: cdtool [-cd] <env> <module>")
		fmt.Println("Examples:")
		fmt.Println("  cdtool -cd prod goods")
		fmt.Println("  cdtool prod goods")
		os.Exit(1)
	}

	targetPath := fmt.Sprintf("/opt/mon/modules/%s/%s", env, module)

	if _, err := os.Stat(targetPath); os.IsNotExist(err) {
		// 修改错误输出，避免冒号等特殊字符
		fmt.Fprintf(os.Stderr, "error - path %s does not exist\n", targetPath)
		os.Exit(1)
	}
	fileInfo, err := os.Stat(targetPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error - checking path %s failed: %v\n", targetPath, err)
		os.Exit(1)
	}
	if !fileInfo.IsDir() {
		fmt.Fprintf(os.Stderr, "error - %s is not a directory\n", targetPath)
		os.Exit(1)
	}

	fmt.Printf("cd %s\n", targetPath)
}
